from general import *
'''
Created on Apr 21, 2011

@author: bob
'''

class Manhattan_Obst(object):
    '''
    classdocs
    '''


    def __init__(self, pState):
        '''
        Constructor
        '''
        self.obstacles = {}
        for o in pState.obstacle_locations:
            self.obstacles[o] = 1
            
    def evaluate(self,pState):
        totalCost = 0
        for p in pState.dirt_locations:
            r,d = bestRobotFromPile(p, pState.robots)
            if (r[0]>p[0]):
                xStart,xEnd, yStart, yEnd, yStep = p[0],r[0], p[1], r[1], 1
                if (p[1]>r[1]):
                    yStep = -1
                    yEnd += -2
            else:
                xStart,xEnd, yStart, yEnd, yStep = r[0],p[0], r[1], p[1], 1
                if (r[1]>p[1]):
                    yStep = -1
                    yEnd += -2
#           path 1:
            cost1 = manhattan(r, p)
#            print "y varies"
            for y in range(yStart,yEnd+1, yStep):
#                print y
                if ((xStart, y) in self.obstacles):
                    cost1 += 1
#            print "x varies"
            for x in range(xStart,xEnd+1):
#                print x
                if ((x, yEnd) in self.obstacles):
                    cost1 += 1
#           path 2: 
            cost2 = manhattan(r, p)
#            print "x varies"
            for x in range(xStart,xEnd+1):
#                print x
                if ((x, yStart) in self.obstacles):
                    cost2 += 1
#            print "y varies"
            for y in range(yStart,yEnd+1, yStep):
#                print y
                if ((xEnd, y) in self.obstacles):
                    cost2 += 1
#            print r,p,"cost1:" + str(cost1),"cost2:" + str(cost2)         
            totalCost += min(cost1, cost2)
        return totalCost
                